package com.zhuiyun.pm.client.user.service.impl;

import com.zhuiyun.pm.client.user.entity.ClientUserEntity;
import com.zhuiyun.pm.client.user.mapper.ClientUserMapper;
import com.zhuiyun.pm.client.user.service.ClientUserService;
import com.zhuiyun.pm.config.CommonResult;
import com.zhuiyun.pm.page.MyPageHelper;
import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.page.PageResult;

import com.zhuiyun.pm.utils.Md5Util;
import com.zhuiyun.pm.utils.UploadFileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

import java.security.MessageDigest;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ClientUserServiceImpl
 * @Description TODO对ClientUser做出操作
 * @Author kdj
 * @Date 2023/3/13 20:58
 **/
@Service
public class ClientUserServiceImpl implements ClientUserService {
    //注入
    @Resource
    private ClientUserMapper clientUserMapper;

    /*
     * @Author kdj
     * @Description 获取全部前端登录用户的数据
     * @Date 2023/3/13 21:00
     * @Return com.zhuiyun.pm.page.PageResult
     * @param clientUser 查询参数
     * @param pageRequest 分页条件
     */
    @Override
    public PageResult getClientUserList(ClientUserEntity clientUserEntity, PageRequest pageRequest) {
        //开始分页
        MyPageHelper.startPage(pageRequest);
        Example userExample = new Example(ClientUserEntity.class);
        Example.Criteria userCriteria = userExample.createCriteria();
        // 用户名名不为空则模糊查询
        if (!StringUtils.isBlank(clientUserEntity.getUserName())) {
            userCriteria.andLike("userName", "%" + clientUserEntity.getUserName() + "%");
        }
        // 手机号不为空则模糊查询
        if (!StringUtils.isBlank(clientUserEntity.getUserPhone())) {
            userCriteria.andLike("userPhone", "%" + clientUserEntity.getUserPhone() + "%");
        }
        // 类型查询
        if (clientUserEntity.getUserType() != null) {
            userCriteria.andEqualTo("userType", clientUserEntity.getUserType());
        }
        if (clientUserEntity.getUserStatus() != null) {
            // 状态查询
            userCriteria.andEqualTo("userStatus", clientUserEntity.getUserStatus());
        }
        //未被删除
        userCriteria.andEqualTo("delFlag", 0);
        //查询
        List<ClientUserEntity> clientUserEntityList = clientUserMapper.selectByExample(userExample);
        //进行分页
        PageResult pageResult = MyPageHelper.pageInfo(clientUserEntityList);
        return pageResult;
    }

    /*
     * @Author kdj
     * @Description 前端用户新增
     * @Date 2023/3/14 10:34
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param multipartFile,clientUserEntity
     */
    @Override
    public CommonResult saveClientUser(MultipartFile multipartFile, ClientUserEntity clientUserEntity) {
        // 检查是否已经存在相同的用户名
        // 创建新的条件
        ClientUserEntity clientUserEntity1 = new ClientUserEntity();
        clientUserEntity1.setUserName(clientUserEntity.getUserName());
        if (clientUserMapper.selectCount(clientUserEntity1) > 0) {
            return CommonResult.error("500", "该用户名已经被使用！");
        }
        //图片上传
        String userAvatarOfStr = UploadFileUtils.uploadFile(multipartFile);
        //判断是否上传成功
        if (userAvatarOfStr == null) {
            return CommonResult.error("头像上传失败，请重新添加用户！");
        }
        //添加图片
        clientUserEntity.setUserAvatar(Integer.parseInt(userAvatarOfStr));
        //对密码进行md5加密
        //判断密码是否为空
        String userPassword = clientUserEntity.getUserPassword();
        if (StringUtils.isNotBlank(userPassword)) {
            clientUserEntity.setUserPassword(Md5Util.getMd5DigestAsHex(userPassword));
        }
        // 设置用户状态和删除标记
        clientUserEntity.setUserStatus(0);//用户状态正常
        clientUserEntity.setDelFlag(0);
        // 设置创建时间和更新时间
        Date now = new Date();
        clientUserEntity.setCreateTime(now);
        clientUserEntity.setUpdateTime(clientUserEntity.getCreateTime());

        //添加
        int rows = clientUserMapper.insert(clientUserEntity);
        return rows > 0 ? CommonResult.ok(rows) : CommonResult.error("添加失败");
    }

    /*
     * @Author kdj
     * @Description 修改前端用户根据id
     * @Date 2023/3/14 14:06
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param headFile,clientUserEntity
     */
    @Override
    public CommonResult upClientUserById(MultipartFile headFile, ClientUserEntity clientUserEntity) {
        // 检查参数是否为空
        if (clientUserEntity == null || clientUserEntity.getId() == null) {
            return CommonResult.error("500", "参数不能为空");
        }
        //判断头像是否存在
        //如果存在就不进行修改了
        if (clientUserEntity.getUserAvatar() == null|| clientUserEntity.getUserAvatar() == 0) {
            //图片上传
            String userAvatarOfStr = UploadFileUtils.uploadFile(headFile);
            //判断是否上传成功
            if (userAvatarOfStr == null) {
                return CommonResult.error("500", "头像上传失败，请重新添加用户！");
            }
            //添加图片
            clientUserEntity.setUserAvatar(Integer.parseInt(userAvatarOfStr));
        }

        //设置修改时间
        clientUserEntity.setUpdateTime(new Date());
        // 根据id主键修改
        int result = clientUserMapper.updateByPrimaryKeySelective(clientUserEntity);
        if (result == 1) {
            return CommonResult.ok("修改成功");
        } else if (result == 0) {
            return CommonResult.error("500", "修改失败：未找到需要修改的数据");
        } else {
            return CommonResult.error("500", "修改失败：更新了多条数据");
        }
    }

    /*
     * @Author kdj
     * @Description 修改前端用户状态为封禁
     * @Date 2023/3/14 14:44
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param id,userStatus
     */
    @Override
    public CommonResult upUserStatus(Integer id, Integer userStatus) {
        ClientUserEntity clientUserEntity = clientUserMapper.selectByPrimaryKey(id);
        //查询用户是否存在
        if (clientUserEntity != null) {
            //查询用户的状态
            if (clientUserEntity.getUserStatus() == userStatus) {
                return CommonResult.error("该用户状态不需要修改");
            }
        } else {
            return CommonResult.error("该用户不存在");
        }
        //修改状态
        clientUserEntity.setUserStatus(userStatus);

        //设置修改时间
        clientUserEntity.setUpdateTime(new Date());
        // 根据id主键修改
        int result = clientUserMapper.updateByPrimaryKeySelective(clientUserEntity);
        if (result == 1) {
            return CommonResult.ok("修改成功");
        } else if (result == 0) {
            return CommonResult.error("修改失败：未找到需要修改的数据");
        } else {
            return CommonResult.error("修改失败：更新了多条数据");
        }
    }

    /*
     * @Author kdj
     * @Description 删除前端用户
     * @Date 2023/3/14 15:59
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param id
     */
    @Override
    public CommonResult removeUserById(int id) {
        //创建clientUserEntity
        ClientUserEntity clientUserEntity = new ClientUserEntity();
        //添加条件
        clientUserEntity.setId(id);
        clientUserEntity.setDelFlag(1);
        // 用update方法更新db中的记录，如果删除失败则返回0
        // 根据id主键修改del_flag
        int result = clientUserMapper.updateByPrimaryKeySelective(clientUserEntity);
        if (result == 1) {
            return CommonResult.ok("删除成功");
        } else {
            return CommonResult.error("删除失败");
        }
    }
}